package com.ddedu.controller;

import com.ddedu.common.JsonResult;
import com.ddedu.entity.Staff;

import com.ddedu.service.StaffService;
import com.ddedu.util.OssUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StaffController {
    @Autowired
    private StaffService staffService;

    @RequestMapping("staffAdd.do")
    @ResponseBody
    public JsonResult add(Staff staff) {
        System.out.println(staff);
        staffService.add(staff);
        return new JsonResult(1, "添加成功");
    }

    @RequestMapping("staffAll.do")
    @ResponseBody
    public Map<String, Object> selectAll(Integer page, Integer limit) {
        HashMap<String, Object> map = new HashMap<>();
        List<Staff> list = staffService.selectStaffAll(page, limit);
        long total = ((Page) list).getTotal();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", total);
        map.put("data", list);
        return map;
    }

    @ResponseBody
    @RequestMapping("staffbydid.do")
    public Map<String, Object> selectStaffsById(String d_id, Integer page, Integer limit) {
        int intdid = Integer.parseInt(d_id);
        HashMap<String, Object> map = new HashMap<>();

        PageHelper.startPage(page, limit);

        List<Staff> list = staffService.selectStaffById(intdid);
        long total = ((Page) list).getTotal();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", total);
        map.put("data", list);
        return map;
    }

    @ResponseBody
    @RequestMapping("/staffdelete.do")
    public JsonResult deleteStaff(String u_no) {
        staffService.deleteByU_no(u_no);

        return new JsonResult(1, null);
    }

    //="name"]').val(), did: $('input[name="did"]').val(), email: $(
//            'input[name="email"]').val(), "phone": $('input[name="phone"]').val(), "qq": $('inp
    @ResponseBody
    @RequestMapping("/updatebydid.do")
    public Map<String, Object> upadateIt(MultipartFile file, String phone, String name, String d_id, String email, String qq, Integer id) {
        Staff staff = new Staff();

        staff.setD_id(d_id);
        staff.setdName(name);
        staff.setEmail(email);
        staff.setPhone(phone);
        staff.setQq(qq);
        staff.setId(id);

//        System.out.println(phone);
//        System.out.println(name);
//        System.out.println(i);
       System.out.println(id);
//        System.out.println(qq);
//        System.out.println(email);

        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 1000);
        map.put("msg", "");
        map.put("data", "请上传文件");
        //验证
        if (!file.isEmpty()) {
            //基于OSS实现上传
            String url = null;
            try {
                url = OssUtil.upload(OssUtil.rename(file.getOriginalFilename()), file.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
            staff.setHeading(url);
            staffService.updateStaff(staff);
            map.put("data", url);
            return map;
        } else {
            staffService.updateStaff(staff);
            return map;
        }
    }

}
